/**
 * 系统配置表管理初始化
 */
var Configs = {
    id: "ConfigsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Configs.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '', field: 'configId', visible: true, align: 'center', valign: 'middle',width:30,
                formatter: function (value, row, index) {
                    return index+1;
                }
            },
            {title: '字段名称', field: 'fieldName', visible: true, align: 'center', valign: 'middle'},
            {title: '字段代码', field: 'fieldCode', visible: true, align: 'center', valign: 'middle'},
            {title: '操作', field:'complainId', visible: true, align: 'center', valign: 'middle',formatter:Configs.op}
    ];
};
/*
 * 操作
 * */
Configs.op = function (value,ros,index) {
    if(ros.complaintProgress==0) {
        return "<a href='javascript:void(0)' onclick=\"SktComplain.openSktComplainDetail()\">审核</a>"
    }else {
        return "<a href='javascript:void(0)' onclick=\"Configs.openConfigsDetail()\">详情</a>"
    }
}

/**
 * 检查是否选中
 */
Configs.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Configs.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加系统配置表
 */
Configs.openAddConfigs = function () {
    var index = layer.open({
        type: 2,
        title: '添加系统配置表',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/configs/configs_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看系统配置表详情
 */
Configs.openConfigsDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统配置表详情',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/configs/configs_update/' + Configs.seItem.configId
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看系统配置表详情
 */
Configs.openConfigsDetail2 = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统配置表修改',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/configs/configs_update2/' + Configs.seItem.configId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除系统配置表
 */
Configs.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/configs/delete", function (data) {
            Feng.success("删除成功!");
            Configs.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("configsId",this.seItem.configId);
        ajax.start();
    }
};

/**
 * 查询系统配置表列表
 */
Configs.search = function () {
    var queryData = {};
    queryData['fieldName'] = $("#condition").val();
    Configs.table.refresh({query: queryData});
};
/**
 * 提交添加
 */
Configs.editSubmit = function() {
    var newData = [];
    $('#content div').each(function (i, val) {
        var obj = new Object();
        obj.configId = $(this).attr('id');
        var val = $(this).find("input");
        if (val.length < 2) {
            obj.fieldValue = $(val).val();
        } else {
            obj.fieldValue = $(val[0]).is(':checked') ? '1' : '0';
        }
        newData.push(obj);
    });


    /**
     * 提交添加
     */
    Configs.addSubmit = function() {
        var newData = [];
        $('#content div').each(function (i, val) {
            var obj = new Object();
            obj.configId = $(this).attr('id');
            var val = $(this).find("input");
            if (val.length < 2) {
                obj.fieldValue = $(val).val();
            } else {
                obj.fieldValue = $(val[0]).is(':checked') ? '1' : '0';
            }
            newData.push(obj);
        });
    }

    $.ajax({
        url: Feng.ctxPath + "/configs/updateJc",
        type: "POST",
        contentType : 'application/json;charset=utf-8', // 设置请求头信息
        dataType:"json",
        data: JSON.stringify(newData),
        success: function(data){
            Feng.success("保存成功!");
            Configs.loadData();
        },
        error: function(res){
            Feng.error("保存失败!" + data.responseJSON.message + "!");
        }
    });

}


//加载数据
Configs.loadData = function(){
    var ajax = new $ax(Feng.ctxPath + "/configs/list", function (data) {});
    ajax.set("page",$('#page').val());
    ajax.start();
}
$(function () {
    var defaultColunms = Configs.initColumn();
    var table = new BSTable(Configs.id, "/configs/systemList?page="+$('#page').val(), defaultColunms);
    table.setPaginationType("server");
    Configs.table = table.init();
});